const db = uniCloud.database();
const { result } = require("xzs-utils");
module.exports = {
  _before: async function () {
    // 通用预处理器
    this.startTime = Date.now();
    console.log(this.getHttpInfo(), "this.getHttpInfo()");
    this.query = this.getHttpInfo().queryStringParameters || {};
  },
  get: async function () {
    const { id } = this.query;
    await db
      .collection("mysite-news")
      .doc(id)
      .update({
        $inc: {
          view_count: 1,
        },
      });
    const res = await db
      .collection("mysite-news")
      .where({
        _id: id,
      })
      .get();
    return result(0, "success", res.data[0] || null);
  },
  _after: function (error, result) {
    if (error) {
      throw error; // 如果方法抛出错误，也直接抛出不处理
    }
    result.timeCost = Date.now() - this.startTime;
    return result;
  },
};
